package com.etl.dataflow.common.util;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author dx
 * @since 2022/3/25
 */
public class ExceptionUtil {

    /**
     * 异常堆栈信息转化为字符串形式
     *
     * @param ex
     * @return
     */
    public static String getFullStackTrace(Exception ex) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        ex.printStackTrace(pout);
        String ret = out.toString();
        pout.close();
        try {
            out.close();
        } catch (Exception ignored) {
        }
        ex.printStackTrace();
        return ret;
    }

    /**
     * 异常堆栈信息转化为字符串形式
     *
     * @param e
     * @return
     */
    public static String getFullStackTrace(Throwable e) {
        StringWriter sw = new StringWriter();
        String ret = "";
        try (PrintWriter pw = new PrintWriter(sw, true)) {
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            ret = sw.toString();
        }
        return ret;
    }
}
